package itbour.onetouchshow.activity.version1_3;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;

import com.google.gson.Gson;
import com.umeng.analytics.MobclickAgent;
import com.ys.uilibrary.tab.BottomTabView;

import java.util.ArrayList;
import java.util.List;

import itbour.onetouchshow.App;
import itbour.onetouchshow.R;
import itbour.onetouchshow.activity.login.LoginActivity;
import itbour.onetouchshow.activity.main.MainActivity;
import itbour.onetouchshow.activity.main.MainPresenter;
import itbour.onetouchshow.base.BaseApplication;
import itbour.onetouchshow.bean.HomeDetailslBean;
import itbour.onetouchshow.constants.AppConst;
import itbour.onetouchshow.fragment.challengezone.ChallengeAreaFragment;
import itbour.onetouchshow.fragment.home.HomeFragment;
import itbour.onetouchshow.fragment.home_v1_3.V_1_3HomeFragment;
import itbour.onetouchshow.fragment.me.MeFragment;
import itbour.onetouchshow.fragment.movie.MovieDesignFragment;
import itbour.onetouchshow.utils.SpUtils;

/**
 * Created by Administrator on 2018/2/27.
 */

public class MainActivityV3 extends MainActivity {

    protected Fragment v_1_3homeFrg, design, challengeZone, meFrg;
    private String[] titles;

    @Override
    protected List<Fragment> getFragments() {
        fragments = new ArrayList<>();
        fragments.add(v_1_3homeFrg);
        fragments.add(design);
        if (isShowChallengeTab()) {
            fragments.add(challengeZone);
        }
        fragments.add(meFrg);
        return fragments;
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);       //统计时长
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    public void initView() {
    }

    @Override
    public void loadSucceed(String result) {
        SpUtils.getInstance().saveString(AppConst.APPBASEINFO, result);
        isFrist = false;
        if (currentSlectIndex != -1) {
            viewPager.setCurrentItem(currentSlectIndex, false);
        }
    }


    @Override
    protected void afterCreate(Bundle savedInstanceState) {
        SpUtils.getInstance().saveBoolean("isFirst", false);
        if (mPresenter == null) {
            mPresenter = new MainPresenter(this);
        }
        BaseApplication.instance().registerNetWorkObserver(this);
        mPresenter.getAppBannerInfo();
        mPresenter.getAdDialogInfo();
        // mPresenter.getHomeDetailsData();

        if (!App.initAppInfo) {
            return;
        }
        if (isShowChallengeTab()) {
            titles = new String[]{"首页", "玩大片", "冲顶大赛", "我的"};
        } else {
            titles = new String[]{"首页", "玩大片", "我的"};
        }
//        setImmersionBarBlack();
        initButtomTab(savedInstanceState);
        initParams();

    }


    public boolean isShowChallengeTab() {
        HomeDetailslBean homeDetailslBean = new Gson().fromJson(SpUtils.getInstance().getString(AppConst.APPBASEINFO), HomeDetailslBean.class);
        HomeDetailslBean.AppControl appControl = homeDetailslBean.getAppControl();
        return appControl.getHiddenActivityTab() != 1;
    }

    @Override
    protected List<BottomTabView.TabItemView> getTabViews() {

        for (int i = 0; i < titles.length; i++) {
            switch (i) {
                case 0:
                    BottomTabView.TabItemView tabItemView1 = new BottomTabView.TabItemView(this, titles[0], R.color.tab_normal_white, R.color.tab_selected, R.drawable.tab_home_normal, R.drawable.tab_home_selector);
                    tabItemViews.add(tabItemView1);
                    break;
                case 1:
                    BottomTabView.TabItemView tabItemView2 = new BottomTabView.TabItemView(this, titles[1], R.color.tab_normal_white, R.color.tab_selected, R.drawable.tab_paly_video_normal_v_1_3, R.drawable.tab_paly_video_selector_v_1_3);
                    tabItemViews.add(tabItemView2);
                    break;
                case 2:
                    //设置不同的图标
                    if (isShowChallengeTab()) {
                        BottomTabView.TabItemView tabItemView3 = new BottomTabView.TabItemView(this, titles[2], R.color.tab_normal_white, R.color.tab_selected, R.mipmap.tab_challenge_normal, R.mipmap.tab_challenge_selected);
                        tabItemViews.add(tabItemView3);
                    } else {
                        BottomTabView.TabItemView tabItemView3 = new BottomTabView.TabItemView(this, titles[2], R.color.tab_normal_white, R.color.tab_selected, R.drawable.tab_me_normal_v_1_3, R.drawable.tab_me_seletor_v_1_3);
                        tabItemViews.add(tabItemView3);
                    }

                    break;
                case 3:
                    BottomTabView.TabItemView tabItemView4 = new BottomTabView.TabItemView(this, titles[3], R.color.tab_normal_white, R.color.tab_selected, R.drawable.tab_me_normal_v_1_3, R.drawable.tab_me_seletor_v_1_3);
                    tabItemViews.add(tabItemView4);
                    break;
            }
        }
        return tabItemViews;
    }

    @Override
    public void initParams() {
        v_1_3homeFrg = new V_1_3HomeFragment();
        design = new MovieDesignFragment();
        if (isShowChallengeTab()) {
            challengeZone = new ChallengeAreaFragment();
        }
        meFrg = new MeFragment();
        //设置ViewPager的缓存界面数,默认缓存为2
        viewPager.setOffscreenPageLimit(tabItemViews.size());
        adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return getFragments().get(position);
            }

            @Override
            public int getCount() {
                return getFragments().size();
            }
        };
        viewPager.setAdapter(adapter);
        if (getOnPageChangeListener() != null) {
            viewPager.addOnPageChangeListener(getOnPageChangeListener());
        }
    }

    public void setCurrentPageIsChallenge() {
        if (titles.length == 4) {
            viewPager.setCurrentItem(2);
        }
    }


    public BottomTabView.OnTabItemSelectListener getOnTabItemSelectListener() {
        return new BottomTabView.OnTabItemSelectListener() {
            @Override
            public void onTabItemSelect(int position) {
                currentSlectIndex = position;
                if (App.isLogin()) {
                    viewPager.setCurrentItem(position, true);
                } else {
                    if (titles[position].equals("我的")) {
                        currentPageIndex = viewPager.getCurrentItem();
                        bottomTabView.updatePosition(currentPageIndex);
                        if (AppConst.CURRENT_PAGE == -1) {
                            AppConst.CURRENT_PAGE = 0;
                        }
                        AppConst.CURRENT_ME = position;
                        openActivity(LoginActivity.class);
                    } else {
                        viewPager.setCurrentItem(position, true);
                        Fragment fragment = fragments.get(position);
                        if (fragment instanceof HomeFragment) {
                            ((HomeFragment) fragment).onNetWorkStatusChange(true);
                        }

                        if (fragment instanceof V_1_3HomeFragment) {
                            ((V_1_3HomeFragment) fragment).onNetWorkStatusChange(true);
                        }
                        AppConst.CURRENT_PAGE = position;
                        AppConst.CURRENT_ME = -1;
                    }
                }

            }
        };
    }


    @Override
    public void onNetWorkStatusChange(boolean connected) {
        if (connected && isFrist) {
        }
    }

}
